package com.tsd.jxc.sale.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.goods.entity.JxcGoodsPriceExt;
import com.tsd.jxc.goods.service.JxcGoodsPriceService;
import com.tsd.jxc.sale.dao.JxcSaleQuotationDetailMapper;
import com.tsd.jxc.sale.dao.JxcSaleQuotationMapper;
import com.tsd.jxc.sale.entity.JxcSaleQuotationDetail;
import com.tsd.jxc.sale.entity.JxcSaleQuotationDetailExt;
import com.tsd.jxc.sale.entity.JxcSaleQuotationExt;
import com.tsd.jxc.sale.service.JxcSaleQuotationDetailService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcSaleQuotationDetail 服务实现层
 *
 * @author Hillpool
 * @date 2022/12/12 11:26:43
 */
@Service
public class JxcSaleQuotationDetailServiceImpl extends BaseServiceImpl implements JxcSaleQuotationDetailService {

    @Resource
    private JxcSaleQuotationDetailMapper jxcSaleQuotationDetailMapper;
    @Resource
    private JxcSaleQuotationMapper jxcSaleQuotationMapper;
    @Resource
    private JxcGoodsPriceService jxcGoodsPriceService;

    @Override
    public JxcSaleQuotationDetail getJxcSaleQuotationDetailById(Long id) throws Exception {
        return jxcSaleQuotationDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcSaleQuotationDetailExt queryBySid(String sid) throws Exception {
        return jxcSaleQuotationDetailMapper.selectBySid(sid);
    }

    @Override
    public List<JxcSaleQuotationDetailExt> queryValidByQuotationList(List<String> quotation_list) {
        List<JxcSaleQuotationDetailExt> detailExtList = ThreadPoolTaskUtil.queryInList4Split(quotation_list, list -> {
            JxcSaleQuotationDetailExt params = new JxcSaleQuotationDetailExt();
            params.setDr(0);
            params.setQuotation_list(list);
            return jxcSaleQuotationDetailMapper.selectByEntity(params);
        });
        if (!HlpUtils.isEmptyList(detailExtList)) {
            List<String> goodsList = ListUtil.map(detailExtList, JxcSaleQuotationDetailExt::getGoods_sid);
            List<JxcGoodsPriceExt> priceExtList = jxcGoodsPriceService.queryByGoodsList(goodsList);
            for (JxcSaleQuotationDetailExt detailExt : detailExtList) {
                List<JxcGoodsPriceExt> filters = ListUtil.filter(priceExtList, t -> t.getGoods_sid().equals(detailExt.getGoods_sid()));
                detailExt.setPrice_list(filters);
                detailExt.calcGoodsPrice();
            }
        }
        return detailExtList;
    }

    @Override
    public void deleteJxcSaleQuotationDetails(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcSaleQuotationDetail old = jxcSaleQuotationDetailMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcSaleQuotationDetailMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcSaleQuotationDetail(JxcSaleQuotationDetail record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcSaleQuotationDetailMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcSaleQuotationDetailMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveJxcSaleQuotationDetail(JxcSaleQuotationExt record, SysUser opUser) throws Exception {
        List<JxcSaleQuotationDetailExt> detailExtList = record.getDetail_list();
        super.checkEmptyList(detailExtList, "报价明细不能为空");

        List<Long> existIdList = ListUtil.filterMap(detailExtList, t -> !HlpUtils.isEmpty(t.getId()), JxcSaleQuotationDetailExt::getId);

        JxcSaleQuotationDetailExt params = new JxcSaleQuotationDetailExt();
        params.setDr(0);
        params.setQuotation_sid(record.getSid());
        List<JxcSaleQuotationDetailExt> oldList = jxcSaleQuotationDetailMapper.selectByEntity(params);

        //删除不存在订单明细
        List<Long> delIds = ListUtil.filterMap(oldList, t -> !existIdList.contains(t.getId()), JxcSaleQuotationDetailExt::getId);
        if (!HlpUtils.isEmptyList(delIds)) {
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            jxcSaleQuotationDetailMapper.deleteLogicByIdList(logicVo);
        }

        List<JxcSaleQuotationDetailExt> insertList = new ArrayList<>();
        List<JxcSaleQuotationDetailExt> updateList = new ArrayList<>();
        double totalQty = 0D, totalAmount = 0D;
        for (JxcSaleQuotationDetailExt detailExt : detailExtList) {
            super.checkEmpty(detailExt.getQty(), "第" + detailExt.getSn() + "项明细数量不能为空");
            super.checkEmpty(detailExt.getPrice(), "第" + detailExt.getSn() + "项明细单价不能为空");
            detailExt.setQuotation_sid(record.getSid());
            detailExt.calcAmount();
            totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
            totalAmount = DoubleUtil.add(totalAmount, detailExt.getTotal_amount());
            if (HlpUtils.isEmpty(detailExt.getId())) {
                super.autoInjectBaseData(detailExt, opUser, this.TYPE_CREATE);
                insertList.add(detailExt);
            } else {
                super.autoInjectBaseData(detailExt, opUser, this.TYPE_UPDATE);
                updateList.add(detailExt);
            }
        }
        if (!HlpUtils.isEmptyList(insertList)) {
            ThreadPoolTaskUtil.executeInList4Split(insertList, false, itemList -> jxcSaleQuotationDetailMapper.insertList(itemList));
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcSaleQuotationDetailMapper.updateList(itemList));
        }
        record.setQty(BigDecimal.valueOf(totalQty));
        record.setAmount(BigDecimal.valueOf(totalAmount));
        jxcSaleQuotationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<JxcSaleQuotationDetailExt> findJxcSaleQuotationDetails(Map<String, Object> params, PageBean page) throws Exception {
        return jxcSaleQuotationDetailMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcSaleQuotationDetailExt> findJxcSaleQuotationDetails(Map<String, Object> params) throws Exception {
        return jxcSaleQuotationDetailMapper.selectByParams(params);
    }
}